package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.CommonsController;
import com.tutu.controller.common.CommonController;
import com.tutu.core.Constant;
import com.tutu.entity.vo.BaseCodeVo;
import com.tutu.service.DeviceDataService;
import com.tutu.service.DeviceService;
import com.tutu.service.LogInfoService;
import com.tutu.utils.BaseUtil;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 设备管理
 */
@Controller
@RequestMapping(value = "backend/device")
public class DeviceController extends CommonController {

    @Autowired
    DeviceService deviceService;

    @Autowired
    DeviceDataService deviceDataService;

    @Autowired
    LogInfoService logInfoService;

    @RequestMapping(value = "/index")
    public String index(Model model) {
        model.addAttribute("deviceCount", CommonsController.baseDeviceList.size());

        return "backend/device/list";
    }

    @RequestMapping(value = "/dataInfo")
    public String dataInfo(Model model, Integer id) {
        model.addAttribute("device", deviceService.getInfoById(id));

        return "backend/device/dataInfo";
    }

    @RequestMapping(value = "/child/index")
    public String childIndex(Model model, String deviceCode, Integer pageNum) {
        model.addAttribute("device", deviceService.getInfoByCode(deviceCode));
        model.addAttribute("pageNum", pageNum);

        return "backend/device/childList";
    }

    @RequestMapping("/list")
    public void list(HttpServletRequest request, HttpServletResponse response, Integer searchCompanyId, String keyWord, Integer deviceType, Integer stateType, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);
        String companyId = request.getSession().getAttribute(Constant.CURRENT_LOGIN_COMPANY_ID) + "";

        if (null != searchCompanyId) companyId = searchCompanyId + "";

        Map<String, Object> result = DataTableFactory.fitting(draw, deviceService.pageByParams(WebUtil.getSysInfo(request, Constant.CURRENT_LOGIN_COMPANY_ID), BaseUtil.isBlank(companyId) ? null : Integer.parseInt(companyId), keyWord, deviceType, stateType, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HttpServletRequest request, Integer id, Integer deviceType, Integer stateType, Integer companyId, String name, String code, Integer showIndex) {
        return deviceService.saveInfo(request, id, deviceType, stateType, companyId, name, code, showIndex);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> del(HttpServletRequest request, Integer companyId, Integer id) {
        return deviceService.deleteById(request, companyId, id);
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public Map<String, Object> batchDel(HttpServletRequest request, Integer companyId, String ids) {
        return deviceService.batchDel(request, companyId, ids);
    }

    @RequestMapping("/findListByCompanyId")
    @ResponseBody
    public List<BaseCodeVo> findListByCompanyId(Integer companyId) {
        return deviceService.findListByCompanyId(companyId);
    }

    @RequestMapping("/getInfo")
    @ResponseBody
    public Map<String, Object> getInfo(String deviceCode, Integer type) {
        return deviceDataService.getInfoByDeviceCodeAndType(deviceCode, type);
    }

    @RequestMapping("/findDeviceEmissionInfo")
    @ResponseBody
    public List<Map<String, Object>> findDeviceEmissionInfo(String deviceCode) {
        return deviceService.findDeviceEmissionInfo(deviceCode);
    }

    @RequestMapping("/updateDeviceEmission")
    @ResponseBody
    public Map<String, Object> updateDeviceEmission(HttpServletRequest request, String deviceCode, String codeNameStr, String countStr, String dayStr) {
        return deviceService.updateDeviceEmission(request, deviceCode, codeNameStr, countStr, dayStr);
    }

    @RequestMapping("/changeCodeStatus")
    @ResponseBody
    public Map<String, Object> changeCodeStatus(HttpServletRequest request, String deviceCode, Integer type, String code, Integer status) {
        return deviceDataService.changeDeviceCodeStatus(request, deviceCode, type, code, status, true);
    }

    @RequestMapping("/changeCodeName")
    @ResponseBody
    public Map<String, Object> changeCodeName(HttpServletRequest request, String deviceCode, Integer type, String code, String content) {
        return deviceDataService.changeDeviceCodeName(request, deviceCode, type, code, content);
    }

    @RequestMapping("/saveForward")
    @ResponseBody
    public Map<String, Object> saveForward(HttpServletRequest request, Integer id, String address, String port) {
        return deviceService.saveForward(request, id, address, port);
    }

    @RequestMapping("/sync")
    @ResponseBody
    public Map<String, Object> sync(HttpServletRequest request) {
        return deviceService.syncData(request);
    }

    @RequestMapping("/syncDeviceShare")
    @ResponseBody
    public Map<String, Object> syncDeviceShare(HttpServletRequest request) {
        return deviceService.syncDeviceShare(request);
    }

    @RequestMapping("/syncDeviceUser")
    @ResponseBody
    public Map<String, Object> syncDeviceUser(HttpServletRequest request) {
        return deviceService.syncDeviceUser(request);
    }

    @RequestMapping("/updateShowStatus")
    @ResponseBody
    public Map<String, Object> updateShowStatus(HttpServletRequest request, String deviceCode, Integer type) {
        return deviceService.updateShowStatus(request, deviceCode, type);
    }

    @RequestMapping("/updateZeroAlarmStatus")
    @ResponseBody
    public Map<String, Object> updateZeroAlarmStatus(HttpServletRequest request, String deviceCode, Integer status) {
        return deviceService.updateZeroAlarmStatus(request, deviceCode, status);
    }

    @RequestMapping("/updateAlarmStatus")
    @ResponseBody
    public Map<String, Object> updateAlarmStatus(HttpServletRequest request, String deviceCode, Integer status) {
        return deviceService.updateAlarmStatus(request, deviceCode, status);
    }

    @RequestMapping("/refreshDeviceStaticData")
    @ResponseBody
    public Map<String, Object> refreshDeviceStaticData(HttpServletRequest request) {
        return deviceService.refreshDeviceStaticData(request);
    }
}